package com.wno704.intf.aspect;

import com.wno704.system.util.IPUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;

/**
 * @ClassName RequestLimitAspect
 * @Description TODO
 * @Author wno704
 * @Date 2022/10/15 17:34
 * @Version 1.0
 */

@Aspect
@Component
@Slf4j
public class RequestcheckAspect {

    @Pointcut("@annotation(com.wno704.intf.aspect.RequestCheck)")
    public void limitPointCut() {
    }

    @Around("limitPointCut()")
    public Object begin(ProceedingJoinPoint joinPoint) throws Throwable {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) requestAttributes;
        if (sra == null) {
            return null;
        }

        HttpServletRequest request = sra.getRequest();
        //HttpServletResponse response = sra.getResponse();

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        // 执行方法
        Method method = signature.getMethod();
        RequestCheck commentCheck = method.getAnnotation(RequestCheck.class);
        if (commentCheck != null) {
            String intfName = commentCheck.intfName();
            boolean check = commentCheck.check();
            String msg = commentCheck.msg();
            String ipAddr = IPUtil.getIpAddr(request);


            log.info(ipAddr + "  intfName:" + intfName + "  msg:" + msg + "  check:" + check);
//            Object cacheObj = CacheUtil.get(cacheKey);
//            System.out.println(ipAddr + "  cacheName:" + cacheName + "  msg:" + msg + "  time:" + time);
//            if (cacheObj != null) {
//                // this.print(response, msg);
//                System.out.println(ipAddr + "  cacheName:" + cacheName + "  msg:" + "  被限制了");
//                return AjaxResult.error(msg);
//            }
//
//            System.out.println(ipAddr + "  cacheName:" + cacheName + "  msg:" + "  初次使用");
//            CacheUtil.put(cacheKey, ipAddr, time * 1000);
        }
        return joinPoint.proceed();
    }

    private void print(HttpServletResponse response, String result) throws IOException {
        if (response != null) {
            response.setContentType("application/json;charset=UTF-8");
            response.getOutputStream().write(result.getBytes(StandardCharsets.UTF_8));
        }
    }

}
